package com.acce.gxsc.page.registerCar.editCompanyInfo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.acce.gxsc.R;
import com.acce.gxsc.model.loaclBean.CompanyBean;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.widget.AcceBar;
import com.acce.sdk.widget.EditWithTitle;
import com.acce.sdk.widget.adapterrv.CommonAdapter;
import com.acce.sdk.widget.adapterrv.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by huxley on 2017/5/26.
 */

public class EditCompanyInfoActivity extends BaseMvpActivity<EditCompanyInfoView, EditCompanyInfoPresenter> implements EditCompanyInfoView {

    @Bind(R.id.accebar)
    AcceBar       mAccebar;
    @Bind(R.id.rv_company_1)
    RecyclerView  mRvCompany1;
    @Bind(R.id.rv_company_2)
    RecyclerView  mRvCompany2;
    @Bind(R.id.ewt_father)
    EditWithTitle mEwtFather;
    @Bind(R.id.ewt_son)
    EditWithTitle mEwtSon;

    ArrayList<CompanyBean> mCompanys = new ArrayList<>();

    CommonAdapter<CompanyBean> mCompanyAdapter1;
    CommonAdapter<CompanyBean> mCompanyAdapter2;

    int position = -1;

    @Override
    protected EditCompanyInfoPresenter createPresenter() {
        return new EditCompanyInfoPresenter();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_company_info);
        ButterKnife.bind(this);
        init();
        mPresenter.loadCompanyList();
    }

    private void init() {
        mRvCompany1.setLayoutManager(new LinearLayoutManager(this));
        mRvCompany1.setAdapter(mCompanyAdapter1 = new CommonAdapter<CompanyBean>(this, R.layout.item_company, mCompanys) {
            @Override
            protected void convert(ViewHolder holder, final CompanyBean saleSheet, final int position) {
                holder.setText(R.id.tv, saleSheet.companyNick);
                holder.setBackgroundColor(R.id.tv, EditCompanyInfoActivity.this.getResources().getColor(EditCompanyInfoActivity.this.position == position ? R.color.divider_dark : R.color.white));
                holder.setOnClickListener(R.id.ll_content, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (EditCompanyInfoActivity.this.position != position) {
                            EditCompanyInfoActivity.this.position = position;
                            mEwtFather.setContent(mCompanys.get(position).companyNick);
                            mEwtSon.setContent("");
                            mCompanyAdapter2.getDatas().clear();
                            mCompanyAdapter2.getDatas().addAll(mCompanys.get(position).sonList);
                            mCompanyAdapter2.notifyDataSetChanged();
                            mCompanyAdapter1.notifyDataSetChanged();
                        }
                    }
                });
            }
        });
        mRvCompany2.setLayoutManager(new LinearLayoutManager(this));
        mRvCompany2.setAdapter(mCompanyAdapter2 = new CommonAdapter<CompanyBean>(this, R.layout.item_company, new ArrayList<CompanyBean>()) {
            @Override
            protected void convert(ViewHolder holder, final CompanyBean saleSheet, final int position) {
                holder.setText(R.id.tv, saleSheet.companyNick);
                holder.setOnClickListener(R.id.ll_content, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mEwtSon.setContent(saleSheet.companyNick);
                    }
                });
            }
        });
    }

    @Override
    public void loadCompanyListSuccess(List<CompanyBean> datas) {
        mCompanyAdapter1.getDatas().clear();
        mCompanyAdapter1.getDatas().addAll(datas);
        mCompanyAdapter2.getDatas().clear();
        mCompanyAdapter2.getDatas().addAll(datas.get(0).sonList);
        mCompanyAdapter1.notifyDataSetChanged();
        mCompanyAdapter2.notifyDataSetChanged();
    }

    @OnClick(R.id.btn_save)
    public void onClick() {
        Intent intent = new Intent();
        intent.putExtra("father", mEwtFather.getContent());
        intent.putExtra("son", mEwtSon.getContent());
        EditCompanyInfoActivity.this.setResult(Activity.RESULT_OK, intent);
        EditCompanyInfoActivity.this.finish();
    }
}
